package com.lsz.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsz.crm.base.BaseService;
import com.lsz.crm.enums.DevResult;
import com.lsz.crm.enums.StateStatus;
import com.lsz.crm.mapper.SaleChanceMapper;
import com.lsz.crm.pojo.SaleChance;
import com.lsz.crm.query.SaleChanceQuery;
import com.lsz.crm.utils.AssertUtil;
import com.lsz.crm.utils.PhoneUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/10 15:45
 * @Version 1.0
 */

@Service
public class SaleChanceService extends BaseService<SaleChance,Integer> {

    @Autowired
    private SaleChanceMapper saleChanceMapper;

    /**
     * 根据多条件分页查询营销机会
     * @param saleChanceQuery
     * @return
     */
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery){

        Map<String,Object> map = new HashMap<>();

        // 开启分页,参数为当前页码，与每页显示的条数
        PageHelper.startPage(saleChanceQuery.getPage(),saleChanceQuery.getLimit());
        // 得到分页对象
        PageInfo<SaleChance> pageInfo = new PageInfo<>(saleChanceMapper.selectByParams(saleChanceQuery));

        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());
        return map;
    }

    /**
     * 添加营销机会
     * @param saleChance
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addSaleChance(SaleChance saleChance){
        // 1.参数校验
        checkSaleChanceParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());
        // 2.设置默认值
        saleChance.setIsValid(1);
        saleChance.setCreateDate(new Date());
        saleChance.setUpdateDate(new Date());
        // 判断是否设置了指派人
        if(StringUtils.isBlank(saleChance.getAssignMan())){
            // 未设置指派人
            // 分配状态
            saleChance.setState(StateStatus.UNSTATE.getType());
            // 指派时间
            saleChance.setAssignTime(null);
            // 开发状态
            saleChance.setDevResult(DevResult.UNDEV.getStatus());
        }else {
            // 设置了指派人
            // 分配状态
            saleChance.setState(StateStatus.STATED.getType());
            // 指派时间
            saleChance.setAssignTime(new Date());
            // 开发状态
            saleChance.setDevResult(DevResult.DEVING.getStatus());
        }
        // 3. 执行添加操作
        AssertUtil.isTrue(saleChanceMapper.insertSelective(saleChance) != 1,"添加失败！");

    }


    /**
     * 更新营销机会
     * @param saleChance
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateSaleChance(SaleChance saleChance){
        /* 1.参数校验 */
        // 营销机会id判断非空
        AssertUtil.isTrue(saleChance.getId() == null,"待更新记录不存在！");
        // 根据id查询营销机会记录
        SaleChance temp = saleChanceMapper.selectByPrimaryKey(saleChance.getId());
        AssertUtil.isTrue(null == temp,"待更新记录不存在！");
        // 参数校验
        checkSaleChanceParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());

        /* 2.设置相关参数的默认值 */
        saleChance.setUpdateDate(new Date());
        // 判断原始数据是否存在
        if(StringUtils.isBlank(temp.getAssignMan())){ //不存在
            // 判断修改后是否存在
            if(!StringUtils.isBlank(saleChance.getAssignMan())){
                // 修改前为空，修改后不空
                saleChance.setAssignTime(new Date());
                saleChance.setState(StateStatus.STATED.getType());
                saleChance.setDevResult(DevResult.DEVING.getStatus());
            }else {
                saleChance.setAssignTime(temp.getAssignTime());
            }
        }else{
            // 存在
            if(StringUtils.isBlank(saleChance.getAssignMan())){ //修改前有值，修改后无值
                saleChance.setAssignTime(new Date());
                saleChance.setState(StateStatus.UNSTATE.getType());
                saleChance.setDevResult(DevResult.UNDEV.getStatus());
            }else { // 修改前有值，修改后有值
                // 判断修改前后是否是同一个用户
                if(!saleChance.getAssignMan().equals(temp.getAssignMan())){ // 不是同一个用户
                    saleChance.setAssignTime(new Date());
                }else { // 是同一个用户
                    saleChance.setAssignTime(temp.getAssignTime());
                }
            }
        }
         /* 执行更新操作 */
        AssertUtil.isTrue(saleChanceMapper.updateByPrimaryKeySelective(saleChance) != 1,"更新失败！");

    }

    /**
     * 删除营销机会
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteSaleChance(Integer[] ids){
        // 判断 ids 是否为空
        AssertUtil.isTrue(null == ids,"待删除记录不存在！");
        // 删除操作
        AssertUtil.isTrue(saleChanceMapper.deleteBatch(ids) != ids.length,"营销机会删除失败！");
    }

    /**
     * 更新营销机会的开发状态
     * @param id
     * @param devResult
     */
    public void updateSaleChanceDevResult(Integer id, Integer devResult) {
        // 判断 id
        AssertUtil.isTrue(null == id,"待更新记录不存在！");
        SaleChance saleChance = saleChanceMapper.selectByPrimaryKey(id);
        // 判断对象
        AssertUtil.isTrue(saleChance == null,"待更新记录不存在！");
        assert saleChance != null;
        saleChance.setDevResult(devResult);
        // 执行更新操作
        AssertUtil.isTrue(saleChanceMapper.updateByPrimaryKeySelective(saleChance) != 1,"更新营销机会开发状态失败！");
    }

    /**
     * 参数检验方法
     * @param customerName
     * @param linkMan
     * @param linkPhone
     */
    private void checkSaleChanceParams(String customerName, String linkMan, String linkPhone) {
        // customName 非空
        AssertUtil.isTrue(StringUtils.isBlank(customerName),"客户名称不能为空！");
        // linkMan 非空
        AssertUtil.isTrue(StringUtils.isBlank(linkMan),"联系人不能为空！");
        // linkPhone 非空
        AssertUtil.isTrue(StringUtils.isBlank(linkPhone),"手机号不能为空！");
        // 判断手机号格式
        AssertUtil.isTrue(!PhoneUtil.isMobile(linkPhone),"手机号码格式不正确！");
    }



}

